clear
cd "C:\Users\amel\Documents\Political Effects of Land-Use Regulation" /*change path as needed*/
log using log.txt, t replace
use data
encode state, gen(id)
sort id year
xtset id year

/*rescaling variables*/
replace pcpi=pcpi/10000
replace qh=qh/1000

/*generating state ideology variable*/
pca mood-pvi
predict f1

/*figure 2*/
gen dcpi20=cpi-L20.cpi
gen dpvi20=f1-L20.f1
scatter dpvi20 L.dcpi20 if year==2008, xtitle("CPI Change, 1987-2007") ytitle("Left Ideology Change, 1988-2008") mlabel(statelabel) mlabsize(vsmall) msize(0) mlabpos(0) || lfit dpvi20 L.dcpi20 if year==2008

/*preparing DID*/
gen period=.
replace period=1 if year==1984
replace period=2 if year==1988
replace period=3 if year==1992
replace period=4 if year==1996
replace period=5 if year==2000
replace period=6 if year==2004
replace period=7 if year==2008
replace period=8 if year==2012
replace period=9 if year==2016

replace dpvi=pvi-L4.pvi
replace cpi=(spp/L.spp)*L.cpi if missing(cpi)

/*figure 5*/
xtreg dpvi i.period i.period#c.L4.cpi L4.pvi, fe vce(cl id)
est store didA

replace north=L.north if missing(north)
replace ncent=L.ncent if missing(ncent)
replace west=L.west if missing(west)
replace south=L.south if missing(south)

xtreg dpvi i.period i.period#c.L4.cpi L4.pvi L8.pvi north ncent south, re vce(cl id)
est store didB

coefplot (didA, label(State FE's)) (didB, label(Parallel Trends)), drop(_cons 2.period 3.period 4.period 5.period 6.period 7.period 8.period 9.period L4.pvi L8.pvi north ncent south) xline(0) levels(99 95 90)

/*with per capita income, appendix table 4*/

xtreg dpvi i.period i.period#c.L4.cpi i.period#c.L4.pcpi L4.pvi, fe vce(cl id)
xtreg dpvi i.period i.period#c.L4.cpi i.period#c.L4.pcpi L4.pvi L8.pvi north ncent south, re vce(cl id)

/*figure 7*/
clear

shp2dta using counties10c, data(counties) coord(coords) genid(id) gencentroids(c) replace
use counties
drop if state=="Hawaii"

sort id

spmat idist cw2 x_c y_c, id(id) norm(row) vtrunc(0.6800791) replace
spmat export cw2 using cw2.txt, replace
spmat sum cw2, links
spwmatrix import using cww.txt, wname(cww) text

replace x_c=x_c+180

gen drep9200=rep2p00-rep2p92
gen drep0008=rep2p08-rep2p00
gen drep0816=rep2p16-rep2p08
gen valinc90=medval90/medinc89

/*de-meaning and logging*/
gen drepm=(drep9200+drep0008+drep0816)/3
gen repm=(rep2p92+rep2p00+rep2p08)/3
gen valim=(ln(valinc90+1)+ln(valinc00+1)+ln(valinc10+1))/3
gen medim=(medinc89+medinc00+medinc10)/3
replace drep9200=drep9200-drepm
replace drep0008=drep0008-drepm
replace drep0816=drep0816-drepm
replace rep2p92=rep2p92-repm
replace rep2p00=rep2p00-repm
replace rep2p08=rep2p08-repm
replace valinc90=ln(valinc90+1)-valim
replace valinc00=ln(valinc00+1)-valim
replace valinc10=ln(valinc10+1)-valim
replace medinc89=medinc89-medim
replace medinc00=medinc00-medim
replace medinc10=medinc10-medim

splagvar drep9200 drep0008 drep0816 , wname(cww) wfrom(Stata) ind(rep2p92 rep2p00 rep2p08 valinc90 valinc00 valinc10 medinc89 medinc00 medinc10)

spreg gs2sls drep9200 rep2p92 wx_rep2p92 valinc90 , elmat(cw2) id(id) het
est store A
spreg gs2sls drep0008 rep2p00 wx_rep2p00 valinc00 , elmat(cw2) id(id) het
est store B
spreg gs2sls drep0816 rep2p08 wx_rep2p08 valinc10 , elmat(cw2) id(id) het
est store C
coefplot (A, label(1992-2000)) (B, label(2000-2008)) (C, label(2008-2016)), keep(valinc*) xline(0) levels(99 95 90)

/*figures 8 & 9*/
clear

use counties10d

egen zon_s=std(avg_wrluri)
sum zon_s
replace valinc10=ln(valinc10+1)
replace medinc00=medinc00/1000
gen dens00=(pop00/1000)/landarea

/*figure 8*/
corr rep2p92 zon_s
scalar r1=r(rho)
corr rep2p96 zon_s
scalar r2=r(rho)
corr rep2p00 zon_s
scalar r3=r(rho)
corr rep2p04 zon_s
scalar r4=r(rho)
corr rep2p08 zon_s
scalar r5=r(rho)
corr rep2p12 zon_s
scalar r6=r(rho)
corr rep2p16 zon_s
scalar r7=r(rho)

matrix Rs=(r1,1992\r2,1996\r3,2000\r4,2004\r5,2008\r6,2012\r7,2016)
mat colnames Rs = rho year
svmat Rs, names(col)
graph bar (asis) rho, over(year) ytitle("Correlation (Rho)")

/*validity of value-income ratio*/
sum zon_s if count_>1 & count_<.
corr valinc10 zon_s if count_>1 & count_<.

/*figure 9*/
sort statefp10 
reg drep0412 zon_s rep2p04 i.statefp10 dens00, cluster(statefp10)
est store a1
reg drep0412 zon_s rep2p04 i.statefp10 dens00 if count_>1 & count_<., cluster(statefp10)
est store a2
reg drep0412 zon_s rep2p04 i.statefp10 dens00 medinc00, cluster(statefp10)
est store b1
reg drep0412 zon_s rep2p04 i.statefp10 dens00 medinc00 if count_>1 & count_<., cluster(statefp10)
est store b2
reg drep0412 zon_s rep2p04 i.statefp10 college00 dens00, cluster(statefp10)
est store c1
reg drep0412 zon_s rep2p04 i.statefp10 college00 dens00 if count_>1 & count_<., cluster(statefp10)
est store c2
gen drep0416=rep2p16-rep2p04
reg drep0416 zon_s rep2p04 i.statefp10 dens00, cluster(statefp10)
est store a3
reg drep0416 zon_s rep2p04 i.statefp10 dens00 if count_>1 & count_<., cluster(statefp10)
est store a4
reg drep0416 zon_s rep2p04 i.statefp10 dens00 medinc00, cluster(statefp10)
est store b3
reg drep0416 zon_s rep2p04 i.statefp10 dens00 medinc00 if count_>1 & count_<., cluster(statefp10)
est store b4
reg drep0416 zon_s rep2p04 i.statefp10 college00 dens00, cluster(statefp10)
est store c3
reg drep0416 zon_s rep2p04 i.statefp10 college00 dens00 if count_>1 & count_<., cluster(statefp10)
est store c4

coefplot (a1, label(All counties, 2004-12)) (a2, label(Limited counties, 2004-12)) (a3, label(All counties, 2004-16)) (a4, label(Limited counties, 2004-16)) , keep(zon_s) xline(0) levels(99 95 90)
coefplot (c1, label(All counties, 2004-12)) (c2, label(Limited counties, 2004-12)) (c3, label(All counties, 2004-16)) (c4, label(Limited counties, 2004-16)) , keep(zon_s college00) xline(0) levels(99 95 90)

/*figure 10*/
clear

shp2dta using nesubs2, data(nesubs) coord(necoords) genid(id) gencentroids(c) replace
use nesubs
/*GeoDa created a bunch of zeros*/
drop wrluri
drop if medval0812==0

sort id

spmat contiguity cwne using necoords, id(id) norm(row) saving(nesubsn.txt, replace) replace
spmat sum cwne, links detail

/*dropping islands*/
drop if id==271 | id==783 | id==800 | id==999 | id==1068 | id==1149 | id==1247 | id==1257 | id==1270 | id==1294 | id==1296 | id==1366
spmat contiguity cwne using necoords, id(id) norm(row) saving(nesubsn.txt, replace) replace
spmat sum cwne, links detail
spmat save cwne using cwne.spmat, replace
spmat export cwne using cwne.txt, replace

sort statea countya cty_suba
merge 1:1 statea countya cty_suba using newrluri, keepusing(wrluri)
drop if _merge==2
drop _merge

sum x_c y_c
spmat idist dwne x_c y_c, id(id) norm(row) vtrunc(2.376125) replace
spmat export dwne using dwne.txt, replace
spmat sum dwne, links

replace x_c=x_c+180

/*pop density*/
gen dens90=total90/ALAND
gen dens00=total00/ALAND
gen dens10=total10/ALAND

egen zon_s=std(wrluri)

spmat lag double wrp2p08 cwne rp2p08
spmat lag double wblack cwne black00
spmat lag double whisp cwne hisp00
spmat lag double wvi cwne valinc00
spmat lag double wdens cwne dens00
spmat lag double wwhite cwne white00
spmat lag double wmi cwne medinc99

xi: reg drp0816 rp2p08 wrp2p08 zon_s i.statea, cluster(statea)
est store A
xi: reg drp0816 rp2p08 wrp2p08 zon_s medinc99 wmi white00 wwhite black00 wblack hisp00 whisp dens00 wdens i.statea, cluster(statea)
est store B

spmat lag double wrp2p00 cwne rp2p00
gen drp0016=rp2p16-rp2p00
xi: reg drp0016 rp2p00 wrp2p00 zon_s i.statea, cluster(statea)
est store A2
xi: reg drp0016 rp2p00 wrp2p00 zon_s medinc99 wmi white00 wwhite black00 wblack hisp00 whisp dens00 wdens i.statea, cluster(statea)
est store B2
coefplot (A, label(Sparse controls, 2008-16)) (A2, label(Sparse controls, 2000-16)) (B, label(Full controls, 2008-16)) (B2, label(Full controls, 2000-16)), keep(zon_s) xline(0) levels(99 95 90) legend(position(6) ring(3))

log close
